/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年1月26日         Root.Lu
 */

package com.coocaa.launcher.pattern.normal.guide;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.RemoteException;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coocaa.x.framework.utils.Cache;
import com.coocaa.x.service.XServiceManager;
import com.coocaa.x.uipackage.BaseView;
import com.coocaa.x.uipackage.SkinManager;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

public class GuideView extends BaseView implements IGuideView {
    public static class GuideViewManager implements IGuideView {
        private static GuideViewManager instance = new GuideViewManager();

        public static GuideViewManager getInstance() {
            return instance;
        }

        private IGuideView guideView = null;
        private View view = null;

        public void setGuideView(IGuideView iview, View view) {
            guideView = iview;
            this.view = view;
        }

        @Override
        public void showMe(Tip[] tips) {
            // TODO Auto-generated method stub
            List<Tip> _tips = new ArrayList<Tip>();
            for (Tip tip : tips) {
                boolean bRead = false;
                try {
                    bRead = XServiceManager.getLiteConfigService().getBooleanValue(tip.ID, false);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (!bRead)
                    _tips.add(tip);
            }
            Tip[] __tips = null;
            guideView.showMe(_tips.toArray(__tips));
        }

        @Override
        public void hideMe() {
            // TODO Auto-generated method stub
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    view.setVisibility(View.GONE);
                }
            });
        }

        @Override
        public void readTip(Tip tip) {
            // TODO Auto-generated method stub
            try {
                XServiceManager.getLiteConfigService().saveBooleanConfig(tip.ID, true);
            } catch (RemoteException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            guideView.readTip(tip);
        }

        @Override
        public void showMe(Tip[] tips, int gravity, int leftMargin, int topMargin) {
            // TODO Auto-generated method stub
            final FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, gravity);
            params.leftMargin = leftMargin;
            params.topMargin = topMargin;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    view.setLayoutParams(params);
                }
            });
            List<Tip> _tips = new ArrayList<Tip>();
            for (Tip tip : tips) {
                boolean bRead = false;
                try {
                    bRead = XServiceManager.getLiteConfigService().getBooleanValue(tip.ID, false);
                } catch (RemoteException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                if (!bRead)
                    _tips.add(tip);
            }
            if (_tips.size() > 0) {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        view.setVisibility(View.VISIBLE);
                    }
                });
                Tip[] __tips = new Tip[_tips.size()];
                guideView.showMe(_tips.toArray(__tips));
            } else {
                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        // TODO Auto-generated method stub
                        view.setVisibility(View.GONE);
                    }
                });
            }
        }

        public static Tip getTipByKeyCode(int keyCode, Tip[] tips) {
            Tip readTip = null;
            for (Tip tip : tips) {
                if (keyCode == tip.keyCode) {
                    readTip = tip;
                    break;
                }
            }
            return readTip;
        }

        public void restoreTips() {
            Tip[] tips = GuideTips.ALLAPPS_TIPS;
            try {
                for (Tip tip : tips)
                    XServiceManager.getLiteConfigService().saveBooleanConfig(tip.ID, false);
                tips = GuideTips.DESKTOP_TIPS;
                for (Tip tip : tips)
                    XServiceManager.getLiteConfigService().saveBooleanConfig(tip.ID, false);
                tips = GuideTips.EDIT_SAVE_TIPS;
                for (Tip tip : tips)
                    XServiceManager.getLiteConfigService().saveBooleanConfig(tip.ID, false);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private Cache<String, TextView> tipViews = new Cache<String, TextView>();

    private LinearLayout root = null;
    private Context mContext = null;

    public GuideView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
        enableFocusable(false);
        root = new LinearLayout(context);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setPadding(
                (int) SkinManager.getInstance().getDimension(R.dimen.tips_internal_left_padding),
                (int) SkinManager.getInstance().getDimension(
                        R.dimen.tips_internal_top_bottom_padding), (int) SkinManager.getInstance()
                        .getDimension(R.dimen.tips_internal_right_padding), (int) SkinManager
                        .getInstance().getDimension(R.dimen.tips_internal_top_bottom_padding));
        root.setPadding(SkyConstants.Div(36), SkyConstants.Div(33), SkyConstants.Div(41),
                SkyConstants.Div(33));
        addView(root);
        setBackgroundDrawable(SkinManager.getInstance().getDrawable(null, R.drawable.tips_bg));
    }

    @Override
    public void showMe(final Tip[] tips) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
            private TextView createTipView(Tip tip) {
                TextView text = new TextView(mContext);
                text.setCompoundDrawablePadding(SkyConstants.Div(51));
                text.setTextAppearance(mContext, R.style.tips_text_style);
                text.setTextSize(SkyConstants.Dpi(36));
                return text;
            }

            private void updateTipView(TextView view, Tip tip) {
                Drawable textDrawable = null;
                switch (tip.keyCode) {
                    case KeyEvent.KEYCODE_MENU: {
                        textDrawable = SkinManager.getInstance().getDrawable(null,
                                R.drawable.tips_menu);
                        break;
                    }
                    case KeyEvent.KEYCODE_BACK: {
                        textDrawable = SkinManager.getInstance().getDrawable(null,
                                R.drawable.tips_back);
                        break;
                    }
                    case KeyEvent.KEYCODE_ENTER: {
                        textDrawable = SkinManager.getInstance().getDrawable(null,
                                R.drawable.tips_enter);
                        break;
                    }
                    case KeyEvent.KEYCODE_DPAD_DOWN: {
                        textDrawable = SkinManager.getInstance().getDrawable(null,
                                R.drawable.tips_down);
                        break;
                    }
                    case Tip.SAVE_BUTTON: {
                        textDrawable = SkinManager.getInstance().getDrawable(null,
                                R.drawable.tips_save);
                        break;
                    }
                    default:
                        break;
                }

                if (textDrawable != null) {
                    textDrawable.setBounds(0, 0, SkyConstants.Div(80), SkyConstants.Div(80));
                    view.setCompoundDrawables(textDrawable, null, null, null);
                }

                if (tip.msg != null)
                    view.setText(tip.msg);
                else if (tip.msg_resid != -1)
                    view.setText(tip.msg_resid);
                else
                    view.setText("unknown msg!!");
            }

            @Override
            public void run() {
                // TODO Auto-generated method stub
                root.removeAllViews();
                tipViews.clear();
                int length = tips.length;
                for (int i = 0; i < length; i++) {
                    TextView view = tipViews.get(tips[i].ID);
                    if (view == null) {
                        view = createTipView(tips[i]);
                        if (length > 1) {
                            if (i == 0)
                                view.setPadding(0, 0, 0, (int) SkinManager.getInstance()
                                        .getDimension(R.dimen.tips_line_interval));
                            else if (i == length - 1)
                                view.setPadding(
                                        0,
                                        (int) SkinManager.getInstance().getDimension(
                                                R.dimen.tips_line_interval), 0, 0);
                            else
                                view.setPadding(
                                        0,
                                        (int) SkinManager.getInstance().getDimension(
                                                R.dimen.tips_line_interval),
                                        0,
                                        (int) SkinManager.getInstance().getDimension(
                                                R.dimen.tips_line_interval));
                        }
                        tipViews.add(tips[i].ID, view);
                        root.addView(view);
                    }
                    updateTipView(view, tips[i]);
                }
                setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void hideMe() {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                setVisibility(View.GONE);
            }
        });
    }

    @Override
    public void readTip(final Tip tip) {
        // TODO Auto-generated method stub
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                TextView view = tipViews.get(tip.ID);
                if (view != null) {
                    tipViews.remove(tip.ID);
                    root.removeView(view);
                    if (tipViews.size() == 0)
                        hideMe();
                }
            }
        });
    }

    @Override
    public void showMe(Tip[] tips, int gravity, int leftMargin, int topMargin) {
        // TODO Auto-generated method stub
        showMe(tips);
    }
}
